package com.tjec.framework.web.exception;

import com.tjec.common.constant.HttpStatus;
import com.tjec.common.enums.NumCode;
import com.tjec.common.exception.BaseException;
import com.tjec.common.exception.CustomException;
import com.tjec.project.esb.domain.ESBAjaxResult;
import com.tjec.project.esb.domain.EsbResHead;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.security.auth.login.AccountExpiredException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.AccessDeniedException;

/**
 * @author ：cjj
 * @date ：Created in 2020/10/16 16:46
 */
@RestControllerAdvice(basePackages={"com.tjec.project.esb.controller"})
public class ESBExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(ESBExceptionHandler.class);

    /**
     * 基础异常
     */
    @ExceptionHandler(BaseException.class)
    public ESBAjaxResult baseException(BaseException e, HttpServletRequest request, HttpServletResponse response)
    {
        response.addHeader(ESBAjaxResult.STATUS_FLAG, NumCode.ZERO.getCode());
        return ESBAjaxResult.error(new EsbResHead(String.valueOf(HttpStatus.ERROR), e.getMessage()),null);
    }

    /**
     * 业务异常
     */
    @ExceptionHandler(CustomException.class)
    public ESBAjaxResult businessException(CustomException e, HttpServletRequest request, HttpServletResponse response)
    {
        response.addHeader(ESBAjaxResult.STATUS_FLAG, NumCode.ZERO.getCode());
        return ESBAjaxResult.error(new EsbResHead(String.valueOf(HttpStatus.ERROR), e.getMessage()), null);
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public ESBAjaxResult handlerNoFoundException(Exception e, HttpServletRequest request, HttpServletResponse response)
    {
        log.error(e.getMessage(), e);
        response.addHeader(ESBAjaxResult.STATUS_FLAG, NumCode.ZERO.getCode());
        return ESBAjaxResult.error(new EsbResHead(String.valueOf(HttpStatus.NOT_FOUND), "路径不存在，请检查路径是否正确"),null);
    }

    @ExceptionHandler(AccessDeniedException.class)
    public ESBAjaxResult handleAuthorizationException(AccessDeniedException e, HttpServletRequest request, HttpServletResponse response)
    {
        log.error(e.getMessage());
        response.addHeader(ESBAjaxResult.STATUS_FLAG, NumCode.ZERO.getCode());
        return ESBAjaxResult.error(new EsbResHead(String.valueOf(HttpStatus.FORBIDDEN), "没有权限，请联系管理员授权"),null);
    }

    @ExceptionHandler(AccountExpiredException.class)
    public ESBAjaxResult handleAccountExpiredException(AccountExpiredException e, HttpServletRequest request, HttpServletResponse response)
    {
        log.error(e.getMessage(), e);
        response.addHeader(ESBAjaxResult.STATUS_FLAG, NumCode.ZERO.getCode());
        return ESBAjaxResult.error(new EsbResHead(String.valueOf(HttpStatus.ERROR), e.getMessage()),null);
    }

    @ExceptionHandler(UsernameNotFoundException.class)
    public ESBAjaxResult handleUsernameNotFoundException(UsernameNotFoundException e, HttpServletRequest request, HttpServletResponse response)
    {
        log.error(e.getMessage(), e);
        response.addHeader(ESBAjaxResult.STATUS_FLAG, NumCode.ZERO.getCode());
        return ESBAjaxResult.error(new EsbResHead(String.valueOf(HttpStatus.ERROR), e.getMessage()),null);
    }

    @ExceptionHandler(Exception.class)
    public ESBAjaxResult handleException(Exception e, HttpServletRequest request, HttpServletResponse response)
    {
        log.error(e.getMessage(), e);
        response.addHeader(ESBAjaxResult.STATUS_FLAG, NumCode.ZERO.getCode());
        return ESBAjaxResult.error(new EsbResHead(String.valueOf(HttpStatus.ERROR), e.getMessage()),null);
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(BindException.class)
    public ESBAjaxResult validatedBindException(BindException e, HttpServletRequest request, HttpServletResponse response)
    {
        log.error(e.getMessage(), e);
        response.addHeader(ESBAjaxResult.STATUS_FLAG, NumCode.ZERO.getCode());
        String message = e.getAllErrors().get(0).getDefaultMessage();
        return ESBAjaxResult.error(new EsbResHead(String.valueOf(HttpStatus.ERROR), message),null);
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Object validExceptionHandler(MethodArgumentNotValidException e, HttpServletRequest request, HttpServletResponse response)
    {
        log.error(e.getMessage(), e);
        response.addHeader(ESBAjaxResult.STATUS_FLAG, NumCode.ZERO.getCode());
        String message = e.getBindingResult().getFieldError().getDefaultMessage();
        return ESBAjaxResult.error(new EsbResHead(String.valueOf(HttpStatus.ERROR), message),null);
    }

}
